//
//  WMEAlbumHeadView.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/2/21.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEAlbumHeadView.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>

#pragma mark - 专辑的详情页的headView
@interface WMEAlbumHeadView ()
@property (nonatomic, retain)UIImageView *backImg;
//@property (nonatomic, retain)UIView *bgView;
@property (nonatomic, retain)UILabel *topic;//主题
@property (nonatomic, retain)UILabel *count;//个数
@property (nonatomic, retain)UILabel *line;//线段
@property (nonatomic, retain)UILabel *userName;//用户名
@property (nonatomic, retain)NSMutableArray *mArr;//共有专辑 存放数据的数组
@end

@implementation WMEAlbumHeadView

- (void)dealloc
{
    [_mArr release];
    [_dic release];
    [_backImg release];
    [_topic release];
    [_count release];
    [_line release];
    [_userName release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        self.bgView = [[UIView alloc] init];
//        [self addSubview:_bgView];
        //打开用户交互
        self.userInteractionEnabled = YES;
        
        //背景图片
        self.backImg = [[UIImageView alloc] init];
        _backImg.contentMode = UIViewContentModeScaleAspectFill;
        _backImg.clipsToBounds = YES;
        [self addSubview:_backImg];
        [_backImg release];
        
        //
        self.topic = [[UILabel alloc] init];
//        _topic.backgroundColor = [UIColor redColor];
        _topic.textAlignment = NSTextAlignmentCenter;
        _topic.font = [UIFont boldSystemFontOfSize:15];
        _topic.textColor = [UIColor whiteColor];
        [self addSubview:_topic];
        [_topic release];
        
        self.count = [[UILabel alloc] init];
//        _count.backgroundColor = [UIColor yellowColor];
        _count.textAlignment = NSTextAlignmentCenter;
        _count.textColor = [UIColor whiteColor];
        _count.font = [UIFont systemFontOfSize:11];
        [self addSubview:_count];
        [_count release];
        
        self.line = [[UILabel alloc] init];
        _line.backgroundColor = [UIColor whiteColor];
        [self addSubview:_line];
        [_line release];
    }
    return self;
}
- (void)setDic:(NSMutableDictionary *)dic
{
    if (_dic != dic) {
        [_dic release];
        _dic = [dic retain];
        
        NSLog(@"%@", _dic);
        
        NSString *str = _dic[@"covers"][0];
        [_backImg sd_setImageWithURL:[NSURL URLWithString:str]];
        
        
        _topic.text = _dic[@"name"];
        
        _count.text = [NSString stringWithFormat:@"%@张图片 · %@人喜欢", _dic[@"count"], _dic[@"like_count"]];
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    __block __typeof(&*self)ws = self;
    
    
    //背景图片
    [_backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.mas_left).with.offset(0);
        make.right.equalTo(ws.mas_right).with.offset(0);
        make.top.equalTo(ws.mas_top).with.offset(0);
        make.bottom.equalTo(ws.mas_bottom).with.offset(0);
    }];
    //添加高斯模糊 这个是iOS8 以后才推出的 别的版本不知道如何
    UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    
    visualEffectView.frame = _backImg.bounds;
    
    [_backImg addSubview:visualEffectView];
    [visualEffectView release];
    
    //专辑名的位置设置
    [_topic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backImg.mas_top).with.offset(10);
        make.left.equalTo(_backImg.mas_left).with.offset(10);
        make.right.equalTo(_backImg.mas_right).with.offset(-10);
        make.height.equalTo(@20);
    }];
    
    //多少图片 多少人喜欢 的位置 设置
    [_count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_topic.mas_bottom).with.offset(5);
        make.left.equalTo(_backImg.mas_left).with.offset(10);
        make.right.equalTo(_backImg.mas_right).with.offset(-10);
        make.height.equalTo(@10);
    }];
    
    //线 的位置 设置
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_count.mas_bottom).with.offset(10);
        make.left.equalTo(_backImg.mas_left).with.offset(100);
        make.right.equalTo(_backImg.mas_right).with.offset(-100);
        make.height.equalTo(@0.5);
    }];
    
    if([_dic[@"category"] integerValue] != 2)
    {
        [self userposition];
    }else{
        [self userposition2];
    }

    
}

- (void)userposition
{
    UIImageView *user = [[UIImageView alloc] init];
    [self addSubview:user];
    __block __typeof(&*self)bs = self;
    [user mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.centerX.equalTo(bs.mas_centerX);
        make.top.equalTo(_line.mas_bottom).with.offset(15);
    }];
    user.layer.cornerRadius = 20;
    user.clipsToBounds = YES;
    user.userInteractionEnabled = YES;
    user.tag = 2100;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userAction:)];
    [user addGestureRecognizer:tap];
    [user sd_setImageWithURL:[NSURL URLWithString:_dic[@"user"][@"avatar"]]];
    
    self.userName = [[UILabel alloc] init];
    [self addSubview:_userName];
    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.with.equalTo(bs.mas_left).with.offset(10);
        make.right.with.equalTo(bs.mas_right).with.offset(-10);
        make.top.with.equalTo(user.mas_bottom).with.offset(10);
        make.height.equalTo(@10);
    }];
    _userName.text = [NSString stringWithFormat:@"by%@", _dic[@"user"][@"username"]];
    _userName.font = [UIFont systemFontOfSize:11];
    _userName.textColor = [UIColor whiteColor];
    _userName.textAlignment = NSTextAlignmentCenter;
    [tap release];
    [user release];
    [_userName release];
}
- (void)userposition2
{
//    self.mArr = [NSMutableArray array];
    _mArr = _dic[@"members"];
    for (NSInteger i = 0; i < _mArr.count; i++) {
        UIImageView *users = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH - (_mArr.count * 45)) / 2 + 45 * i, 70, 40, 40)];
        [self addSubview:users];
        users.layer.cornerRadius = 20;
        users.clipsToBounds = YES;
        users.userInteractionEnabled = YES;
        users.tag = 2200 + i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userAction2:)];
        [users addGestureRecognizer:tap];
        NSDictionary *tempDic = _mArr[i];
        [users sd_setImageWithURL:[NSURL URLWithString:tempDic[@"avatar"]]];
        [tap release];
        [users release];
    }
    self.userName = [[UILabel alloc] initWithFrame:CGRectMake(10, 120, WIDTH - 20, 10)];
    _userName.text = [NSString stringWithFormat:@"by%@等%@人", _dic[@"user"][@"username"], _dic[@"member_count"]];
    _userName.font = [UIFont systemFontOfSize:11];
    _userName.textColor = [UIColor whiteColor];
    _userName.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_userName];
    [_userName release];
}

- (void)userAction:(UITapGestureRecognizer *)tap
{
    NSString *userTemp = @"http://www.duitang.com/napi/people/profile/?__dtac=%257B%2522_r%2522%253A%2520%2522998121%2522%257D&_ts=1452865118.002704&app_code=gandalf&app_version=5.8%20rv%3A149591&device_name=iPhone%205&device_platform=iPhone5%2C2&include_fields=relationship%2Cbind_status%2Cinterests%2Cidentity%2Ccity%2Ccitycode%2Cgender%2Cbirthday%2Crecommend_info%2Cclub_count&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=568&screen_width=320&user_id=11200118";
    NSString *userStr = [userTemp stringByReplacingOccurrencesOfString:@"11200118" withString:[_dic[@"user"][@"id"] stringValue]];
    //    NSLog(@"%@", userStr);
    //AFN网络请求
    AFHTTPSessionManager *userData = [AFHTTPSessionManager manager];
    [userData GET:userStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        NSLog(@"%@", responseObject);
        NSMutableDictionary *userDic = responseObject[@"data"];
        [self.delegate userPushAction:[_dic[@"user"][@"id"] stringValue] userDic:userDic];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}
- (void)userAction2:(UITapGestureRecognizer *)tap
{
    NSDictionary *tempDic = _mArr[tap.view.tag - 2200];
    NSString *userTemp = @"http://www.duitang.com/napi/people/profile/?__dtac=%257B%2522_r%2522%253A%2520%2522998121%2522%257D&_ts=1452865118.002704&app_code=gandalf&app_version=5.8%20rv%3A149591&device_name=iPhone%205&device_platform=iPhone5%2C2&include_fields=relationship%2Cbind_status%2Cinterests%2Cidentity%2Ccity%2Ccitycode%2Cgender%2Cbirthday%2Crecommend_info%2Cclub_count&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=568&screen_width=320&user_id=11200118";
    NSString *userStr = [userTemp stringByReplacingOccurrencesOfString:@"11200118" withString:[tempDic[@"id"] stringValue]];
    //    NSLog(@"%@", userStr);
    //AFN网络请求
    AFHTTPSessionManager *userData = [AFHTTPSessionManager manager];
    [userData GET:userStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //        NSLog(@"%@", responseObject);
        NSMutableDictionary *userDic = responseObject[@"data"];
        [self.delegate userPushAction:[tempDic[@"id"] stringValue] userDic:userDic];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}

@end
